﻿using System;
using System.Collections;
using System.Collections.Generic;
using Eamon.MeituanDotnetSDK.api.common;
using Eamon.MeituanDotnetSDK.httpclient;
using Eamon.MeituanDotnetSDK.utils;

namespace Eamon.MeituanDotnetSDK.executor
{
    public class TokenExecutor:AbstractExecutor,Executor
    {
        private string signKey;
        private long developerId;
        public TokenExecutor()
        {
        }
        public TokenExecutor(OpenAPIHttpClient httpClient, string signKey, long developerId)
        {
            this.httpClient = httpClient;
            this.signKey = signKey;
            this.developerId = developerId;
        }
        protected override void prepare(ExecutionContext context)
        {

            IAPI api = context.getApi();
            Dictionary<string, object> paramters = api.getAPIRequest().toParams();
            Hashtable executeParams = [];
            executeParams.Add(CommonConstants.CHARSET, CommonConstants.CHARSET_UTF8);
            executeParams.Add(CommonConstants.DEVELOPER_ID, developerId);
            executeParams.Add(CommonConstants.TIMESTAMP, TimeUtil.formatTime(DateTimeOffset.Now));
            foreach (var kv in paramters)
            {
                if (kv.Value is DateTimeOffset v)
                {
                    string date = TimeUtil.formatTime(v);
                    executeParams.Add(kv.Key, date);
                }
                else
                {
                    executeParams.Add(kv.Key, kv.Value);
                }
            }
            string paramSign=EncryptUtil.getSign(signKey, executeParams);
            executeParams.Add(CommonConstants.SIGN, paramSign);
            context.setParams(executeParams);
            context.setUrl(api.getHttpUrl());
        }


    }

}
